//
//  SettingView.swift
//  MyCloset
//
//  Created by 牟好好 on 2024/11/5.
//

import SwiftUI

struct SettingView: View {
    @EnvironmentObject var styleVm: ClothStylesViewModel
    
    var body: some View {
        VStack(spacing: 15) {
            CleanMemory()
            
            Divider()
                .frame(minHeight: 1)
                .background(.gray)
            
            NavigationLink {
                // 为了预览，必须配置环境
                CustomView()
                    .environmentObject(styleVm)
                    .navigationBarBackButtonHidden()
            } label: {
                SettingRowView(title: "修改样式", subTitle: "新增您所期望的服装样式")
            }
            
            Divider()
                .frame(minHeight: 1)
                .background(.gray)
            
            NavigationLink {
                CustomRecommendsView()
                    .environmentObject(styleVm)
                    .navigationBarBackButtonHidden()
            } label: {
                SettingRowView(title: "修改搭配", subTitle: "更改为您所期望的搭配")
            }
            
            Spacer()
        }
        .padding(.horizontal)
    }
}

struct CleanMemory: View {
    @StateObject var fileVm = FileManagerViewModel()
    
    var body: some View {
        VStack(alignment: .leading) {
            HStack(alignment: .top) {
                VStack(alignment: .leading) {
                    Text("缓存数据")
                        .font(.headline)
                    
                    Text(fileVm.tempDirectorySize)
                        .font(.system(size: 40, weight: .bold))
                }
                Spacer()
                Button {
                    fileVm.deleteTempFolder()
                } label: {
                    Text("清理")
                }
                .foregroundStyle(fileVm.tempDirectorySize == "0 MB" ? .gray : .white)
                .padding(.all, 7)
                .background(fileVm.tempDirectorySize == "0 MB" ? .clear : .blue)
                .overlay(
                    RoundedRectangle(cornerRadius: 25)
                        .stroke(fileVm.tempDirectorySize == "0 MB" ? .gray : .clear, lineWidth: 1)
                )
                .clipShape(.capsule)
                .offset(x: -15, y: 5)
            }
            Text("使用时产生的临时数据，如上传照片占用的内存，清理不影响正常使用")
                .font(.footnote)
                .foregroundStyle(.gray)
        }
        .onAppear {
            fileVm.calculateTempDirectorySize()
        }
    }
}

struct SettingRowView: View {
    var title: String
    var subTitle: String
    
    var body: some View {
        HStack(alignment: .center) {
            VStack(alignment: .leading) {
                Text(title)
                    .font(.headline)
                Text(subTitle)
                    .foregroundStyle(.gray)
            }
            Spacer()
            Image(systemName: "chevron.right")
                .foregroundStyle(.gray)
        }
        .foregroundStyle(.accent)
    }
}

#Preview {
    NavigationView {
        SettingView()
            .environmentObject(ClothStylesViewModel())
    }
}

#Preview {
    NavigationView {
        SettingView()
            .environmentObject(ClothStylesViewModel())
            .preferredColorScheme(.dark)
    }
}
